package cl.rizepe.playntest.core.views;

import static playn.core.PlayN.graphics;

import java.util.ArrayList;

import playn.core.Canvas;
import playn.core.CanvasImage;
import playn.core.Image;

public class SpriteComposed extends SpriteGL{
	ArrayList<Image> images = new ArrayList<Image>();
	
	public SpriteComposed(Image image, int widthFrames, int heightFrames) {
		super(image, widthFrames, heightFrames);
		images.add(image);
	}
	
	public int addImage(Image image){
		images.add(image);
		drawImage(image);
		return images.size();
	}

	protected void drawImage(Image image) {
		for (int i=0;i<frames;i++){
			int m=i%widthFrames;
			int n=i/widthFrames;
			CanvasImage canvasImage = graphics().createImage(width, height);
			Canvas canvas = canvasImage.canvas();
			Image added=image.subImage(width*m, height*n, width, height);
			canvas.drawImage(frameList.get(i), 0, 0);
			canvas.drawImage(added, 0, 0);
			
			frameList.set(i, canvasImage);
		}
	}
	
	public int removeImage(int index){
		if (images.size()>index&&index>=0){
			images.remove(index);
		}
		reCompose();
		return images.size();
	}
	
	public int imagesSize(){
		return images.size();
	}
	
	public void reCompose(){
		if (images.size()>0)
		super.initFrameList(images.get(0), widthFrames, heightFrames);
		for (int i=1;i<images.size();i++){
			drawImage(images.get(i));
		}
	}

}
